﻿using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using SharePointListSecurity.Model;

namespace SharePointListSecurity.Layouts.SharePointListSecurity
{
    public partial class ListViewPermissions : LayoutsPageBase
    {
        protected SPSite spServer;
        protected SPWeb spWeb;
        protected SPList spList;
        protected bool fHideStandardViewOption;
        protected bool fHideGridViewOption;
        protected bool fHideCalendarViewOption;
        protected bool fHideRecursiveViewOption;
        protected bool fHideDiscussionViewsOption;
        protected bool fHideRichDbViewOption;
        protected bool fHideGanttViewOption;
        protected bool fHideCalendarViewForNonCalendarOption;
        protected bool fShowCustomViewOption;
        protected bool fColumnPosition;
        protected string RecursiveViewTypeText;
        protected string RecursiveViewTypeDescription;
        protected string urlViewNew;

        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            spWeb = SPControl.GetContextWeb(Context);
            spList = spWeb.Lists.GetList(new Guid(Request.QueryString.GetValues("List")[0]), true);
            PageTarget = spList;

            fHideGridViewOption = (ListViewWebPart.RestrictedTemplateType(spList) || !spList.DoesUserHavePermissions(SPBasePermissions.UseClientIntegration));
            fHideGridViewOption = (fHideGridViewOption || spList.DisableGridEditing);
            fHideStandardViewOption = (spList.BaseTemplate == SPListTemplateType.DiscussionBoard || spList.BaseTemplate == SPListTemplateType.Events);
            fHideDiscussionViewsOption = (spList.BaseTemplate != SPListTemplateType.DiscussionBoard);
            fHideCalendarViewOption = (spList.BaseTemplate != SPListTemplateType.Events);
            fHideCalendarViewForNonCalendarOption = (spList.BaseTemplate == SPListTemplateType.UserInformation) || spList.HasExternalDataSource;
            fHideRichDbViewOption = (spList.BaseTemplate == SPListTemplateType.UserInformation) || !spList.DoesUserHavePermissions(SPBasePermissions.UseClientIntegration);
            fHideGanttViewOption = spList.BaseTemplate == SPListTemplateType.UserInformation || spList.BaseTemplate == SPListTemplateType.DiscussionBoard || spList.HasExternalDataSource || fHideGridViewOption;
            fShowCustomViewOption = (spList.DoesUserHavePermissions(SPBasePermissions.AddAndCustomizePages) && spList.DoesUserHavePermissions(SPBasePermissions.ManageLists));

            fColumnPosition = true;
        }

        protected string RenderPermissionView()
        {
            var html = @"<td class='ms-vb2'>
                      <table>
                          <colgroup>
                              <col style='width: 80px;' />
                              <col />
                          </colgroup>
                          <tr>
                              <td style='vertical-align: top;'></td>
                              <td></td>
                          </tr>
                      </table>
                      </td>";
            return html;
        }


        protected string EditViewPermissionPath(ViewFormat viewName)
        {
            return "/EditViewPermissions.aspx?List=" + spList.ID.ToString() + "&View=" + viewName;
        }

    }
}
